// 验证四位数字
export function isFourDigits(str) {
	let reg = /^[0-9]{4}$/;
	return reg.test(str)
}
// 验证手机号
export function isMobile(str) {
	let reg = /^[0-9]{11}$/;
	return reg.test(str)
}
// 验证邮箱
export function regEmail(str) {
	let reg = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return reg.test(str)
}
// 身份证
export function regIdCard(str) {
	let reg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	return reg.test(str)
}

export function validRequired(val, title = "请填写必填项", alertTip) {
	let reuslt = false;
	if (val == null) reuslt = true;
	if (val === '') reuslt = true;
	if (reuslt && alertTip) {
		uni.showToast({
			title,
			icon: 'none'
		})
	}
	return reuslt;
}

// demo
// validator, 通过返回true，不同通过返回false
// const rules = [
// 	{ prop: 'name', required: true, message: "请选择" },
// 	{ prop: 'name', required: true, message: "请选择" },
// 	{ prop: 'name',r equired: true, message: "请选择" },
//  { prop: 'name', validator: validatorPhone, message: "请选择“ }
// ]
// 使用
// validForm(this.form, [
// 	{ prop: 'refundAmount', required: true, message: "请输入赔款金额" },
// 	{ prop: 'refundReason', required: true, message: "请选择赔款原因" }
// ])
// 验证表单方法，根据rules结合validRequired实现表单验证+提示
export function validForm(form = {}, rules, alertTip = true) {
	if (rules == null) return true;
	for (var i = 0; i < rules.length; i++) {
		const ruleItem = rules[i];
		if (ruleItem.required) {
			const alert = validRequired(form[ruleItem.prop], ruleItem.message, alertTip);
			if (alert) return false;
		} else if (ruleItem.validator) {
			const validItem = ruleItem.validator(form[ruleItem.prop]);
			if (validItem == false) {
				// 是否提示message，
				if(alertTip) {
					uni.showToast({
						title: ruleItem.message,
						icon: 'none'
					})
				}
				return false;
			}
		}
	}
	return true;
}
